package com.gopay.common.cps.dao.gsrt.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.cps.dao.gsrt.GsrtCustTenderMasterDao;
import com.gopay.common.domain.gsrt.GsrtCustTenderMaster;
@Repository("gsrtCustTenderMasterDao")
public class GsrtCustTenderMasterDaoImpl extends 
BaseDAOImpl<GsrtCustTenderMaster, Long> implements GsrtCustTenderMasterDao{
  /**
   * 发标插入基本信息操作	
   */
 @Override
 public  GsrtCustTenderMaster save(GsrtCustTenderMaster master){
		return super.save(master);
	}
 
 /**
	 * 
	 * @param date
	 * @return 生成主订单号
	 */
 @Override
 public String getGopayOrderId_gsrt(String date) {
		String seq = getHibernateTemplate().execute(new HibernateCallback<String>() {
			@Override
			public String doInHibernate(Session session) throws HibernateException, SQLException {
				SQLQuery query = session.createSQLQuery("select ORDER_ID_SEQ.nextval from dual");
				List children = query.list();
				return ((BigDecimal) children.get(0)).toString();
			}
		});

		String orderId = date + StringUtils.leftPad(seq, 8, "0");
		return orderId;
	}
 
 @Override
 public List<GsrtCustTenderMaster> getbyBidNo(String mercustId, String bidNo){
	 String hql="from GsrtCustTenderMaster where  merCustId='"+mercustId+"' and bidNo='"+bidNo+"'";
	 List<GsrtCustTenderMaster> list= this.findByHql(hql);
	 return list;
 }
   
 
 @Override
 public GsrtCustTenderMaster findByMerCustIdAndBidNo(String merCustId,String bidNo ){
     DetachedCriteria criteria = DetachedCriteria.forClass(GsrtCustTenderMaster.class);
     criteria.add(Restrictions.eq("merCustId", merCustId));
     criteria.add(Restrictions.eq("bidNo", bidNo));
     criteria.add(Restrictions.eq("txnStaCd", TxnStaCode.TXN_STA_CD_20000.value));
     List<GsrtCustTenderMaster> list = this.findByCriteria(criteria);
     return list.size()>0 ? list.get(0): null;
 }
   
}
